public class AufgabeWaren8 { public static void main(String[] arg) { String[] waren = new String[]{ "Ayran", "Schokoriegel", "Stifte", "Briefmarken", "Kaugummis" }; int[] lager = new int[]{ 99, 70, 53, 490, 30 }; double[] preise = new double[]{ 1.99, 1.30, 0.99, 0.85, 2.49 }; printLager(waren, lager, preise); } public static void printLager( String[] waren, int[] lager, double[] preis) { int n; double einzelWert = 0.0; for ( n = 0; n < waren.length; n++ ) { einzelWert = lager[n] * preis[n]; print( indent(20, waren[n] )+ ": " + indent( 5, lager[n] )+ " " + indent( 6, preis[n] )+ " Euro " + indent( 6, einzelWert) + " Euro\n" ); } print( indent(22, "Lager Gesamt: ") + indent(25, gesamtWert(lager, preis) ) + " Euro" ); } /* Funktion gesamtWert */ public static double gesamtWert(int[] lager, double[] preis) { double gesamt = 0.0; for (int n = 0; n < lager.length; n++) { gesamt += lager[n] * preis[n]; } return gesamt; } /* Hilfsfunktion indent */ public static String indent(int length, String s) { while ( s.length() < length ) { s = " " + s; } return s; } public static String indent(int length, int s) { String text = s + ""; while ( text.length() < length ) { text = " " + text; } return text; } public static String indent(int length, double s) { // Vermeidet Gleitkommafehler, // schneidet stellen kleiner als // Cent-Beträge ab s = ( Math.floor(s * 100) / 100 ); // s = (double)(int)( s * 100 ) / 100; String text = s + ""; if ( (s * 100) % 10 == 0 ) { text += "0"; } while ( text.length() < length ) { text = " " + text; } return text; } /* Hilfsfunktionen aus früheren Aufgaben */ /* print() und arr() */ public static String print(String p) { System.out.print(p); return p; } public static String arr(int p[]) { int n; String r = ""; for (n = 0; n < p.length; n++) { r += p[n] + ", "; } return r; } public static String arr(String p[]) { int n; String r = ""; for (n = 0; n < p.length; n++) { r += p[n] + "\n"; } return r; } }